package orgs

import (
	"fmt"
	"jgt-sdk/oper"

	"github.com/gin-gonic/gin"
	"github.com/pkg/errors"
	"gogs.xiaoyuanjijiehao.com/antlinker/antcms/app/ginsrv/api/base"
	"gogs.xiaoyuanjijiehao.com/antlinker/antcms/core"
	"gogs.xiaoyuanjijiehao.com/antlinker/antcms/core/columnd"
	"gogs.xiaoyuanjijiehao.com/antlinker/antcms/core/infod"
	"gogs.xiaoyuanjijiehao.com/antlinker/antcms/core/orgd"
	"gogs.xiaoyuanjijiehao.com/antlinker/antcms/log"
)

func createCtrl(d core.CmsDomain) *orgCtrl {
	return &orgCtrl{
		BaseContrl: base.BaseContrl{
			Domain: d,
		},
		info: d.Info(),
		iar:  d.Info().InfoAggregateRoot(),
		car:  d.Column().ColumnAggregateRoot(),
		oar:  d.Org().OrgAggregateRoot(),
	}
}

type orgCtrl struct {
	base.BaseContrl
	info infod.InfoDomain
	iar  infod.InfoAggregateRoot
	car  columnd.ColumnAggregateRoot
	oar  orgd.OrgAggregateRoot
}

type reqOrgDesc struct {
	Icon      []string `json:"icon"`
	Columnids []string `json:"columnids"`
}

func (c orgCtrl) add(ctx *gin.Context) {
	var req reqOrgDesc
	orgid := ctx.Param("orgid")
	if orgid == "" {
		c.ErrBadRequest(ctx, 1, "未设置组织编号")
		return
	}
	if err := ctx.BindJSON(&req); err != nil {
		c.ErrBadRequestByBind(ctx, 1, err)
		return
	}
	dctx := c.DDDContext(ctx)
	desc := orgd.OrgDesc{
		Icon:      req.Icon,
		Columnids: req.Icon,
	}

	err := c.oar.AddOrg(dctx, orgid, desc)
	if err != nil {
		c.ErrServerError(ctx, 6, "保存失败", errors.Wrap(err, "新组织组织失败："))
		return
	}
	c.Success(ctx, nil)
}

func (c orgCtrl) updateDesc(ctx *gin.Context) {
	var req reqOrgDesc
	orgid := ctx.Param("orgid")
	if orgid == "" {
		c.ErrBadRequest(ctx, 1, "未设置组织编号")
		return
	}
	if err := ctx.BindJSON(&req); err != nil {
		c.ErrBadRequestByBind(ctx, 1, err)
		return
	}
	dctx := c.DDDContext(ctx)
	desc := orgd.OrgDesc{
		Icon:      req.Icon,
		Columnids: req.Icon,
	}
	org, err := c.oar.GetOrg(dctx, orgid)
	if err != nil {
		c.ErrServerError(ctx, 3, "保存失败", errors.Wrap(err, "检测组织是否存在失败."))
		return
	}
	if org == nil {
		c.ErrNotFound(ctx, 4, fmt.Sprintf("组织(%v)不存在", orgid))
		return
	}

	err = org.UpdateDesc(dctx, desc)
	if err != nil {
		c.ErrServerError(ctx, 10, "保存失败", errors.Wrapf(err, "修改组织组织(%v)失败.", orgid))
		return
	}
	c.Success(ctx, nil)
}

func (c orgCtrl) updateIcon(ctx *gin.Context) {
	var req reqOrgDesc
	orgid := ctx.Param("orgid")
	if orgid == "" {
		c.ErrBadRequest(ctx, 1, "未设置组织编号")
		return
	}
	if err := ctx.BindJSON(&req); err != nil {
		c.ErrBadRequestByBind(ctx, 1, err)
		return
	}
	dctx := c.DDDContext(ctx)
	org, err := c.oar.GetOrg(dctx, orgid)
	if err != nil {
		c.ErrServerError(ctx, 3, "保存失败", errors.Wrap(err, "检测组织是否存在失败."))
		return
	}
	if org == nil {
		c.ErrNotFound(ctx, 4, fmt.Sprintf("组织(%v)不存在", orgid))
		return
	}

	err = org.UpdateIcon(dctx, req.Icon)
	if err != nil {
		c.ErrServerError(ctx, 10, "保存失败", errors.Wrapf(err, "修改组织展示图片(%v)失败.", orgid))
		return
	}
	c.Success(ctx, nil)
}

func (c orgCtrl) updateColumn(ctx *gin.Context) {
	var req reqOrgDesc
	orgid := ctx.Param("orgid")
	if orgid == "" {
		c.ErrBadRequest(ctx, 1, "未设置组织编号")
		return
	}
	if err := ctx.BindJSON(&req); err != nil {
		c.ErrBadRequestByBind(ctx, 1, err)
		return
	}
	dctx := c.DDDContext(ctx)
	org, err := c.oar.GetOrg(dctx, orgid)
	if err != nil {
		c.ErrServerError(ctx, 3, "保存失败", errors.Wrap(err, "检测组织是否存在失败."))
		return
	}
	if org == nil {
		c.ErrNotFound(ctx, 4, fmt.Sprintf("组织(%v)不存在", orgid))
		return
	}

	err = org.UpdateShowColumn(dctx, req.Columnids)
	if err != nil {
		c.ErrServerError(ctx, 10, "保存失败", errors.Wrapf(err, "修改组织显示栏目(%v)失败.", orgid))
		return
	}
	c.Success(ctx, nil)
}

func (c orgCtrl) delete(ctx *gin.Context) {
	orgid := ctx.Param("orgid")
	if orgid == "" {
		c.ErrBadRequest(ctx, 1, "请求参数错误", nil)
		return
	}
	dctx := c.DDDContext(ctx)
	ia, err := c.oar.GetOrg(dctx, orgid)
	if err != nil {
		c.ErrServerError(ctx, 2, "删除失败", errors.Wrapf(err, "检测组织(%v)是否存在失败：", orgid))
		return
	}
	if ia == nil {
		c.ErrNotFound(ctx, 3, "组织不存在", nil)
		return
	}

	err = ia.Delete(dctx)
	if err != nil {
		c.ErrServerError(ctx, 4, "删除失败", errors.Wrapf(err, "删除组织(%v)失败：", orgid))
		return
	}
	c.Success(ctx, nil)
}
func (c orgCtrl) destroy(ctx *gin.Context) {
	orgid := ctx.Param("orgid")
	if orgid == "" {
		c.ErrBadRequest(ctx, 1, "请求参数错误", nil)
		return
	}
	dctx := c.DDDContext(ctx)
	ia, err := c.oar.GetOrg(dctx, orgid)
	if err != nil {
		c.ErrServerError(ctx, 2, "彻底删除失败", errors.Wrapf(err, "检测组织(%v)是否存在失败：", orgid))
		return
	}
	if ia == nil {
		c.Success(ctx, nil)
		return
	}
	if ia.Status != -1 {
		c.ErrBadRequest(ctx, 3, "未删除不能彻底删除", errors.Wrapf(err, "检测组织(%v)状态不是删除状态：", orgid))
		return
	}
	err = ia.Destroy(dctx)
	if err != nil {
		c.ErrServerError(ctx, 4, "彻底删除失败", errors.Wrapf(err, "彻底删除组织(%v)失败：", orgid))
		return
	}
	c.Success(ctx, nil)
}

// func (c orgCtrl) recover(ctx *gin.Context) {
// 	orgid := ctx.Param("orgid")
// 	if orgid == "" {
// 		c.ErrBadRequest(ctx, 1, "请求参数错误", nil)
// 		return
// 	}
// 	dctx := c.DDDContext(ctx)
// 	ia, err := c.oar.GetOrg(dctx, orgid)
// 	if err != nil {
// 		c.ErrServerError(ctx, 2, "恢复失败", errors.Wrapf(err, "检测组织(%v)是否存在失败：", orgid))
// 		return
// 	}
// 	if ia == nil {
// 		c.Success(ctx, nil)
// 		return
// 	}
// 	if ia.Status != -1 {
// 		c.ErrBadRequest(ctx, 3, "未删除不能恢复", errors.Wrapf(err, "检测组织(%v)状态不是删除状态：", orgid))
// 		return
// 	}
// 	err = ia.Recover(dctx)
// 	if err != nil {
// 		c.ErrServerError(ctx, 4, "恢复失败", errors.Wrapf(err, "恢复组织(%v)失败：", orgid))
// 		return
// 	}
// 	c.Success(ctx, nil)
// }

// 获取指定组织
func (c orgCtrl) getOrg(ctx *gin.Context) {

	orgid := ctx.Param("orgid")
	dctx := c.DDDContext(ctx)
	if orgid == "" {
		c.ErrBadRequest(ctx, 1, "请求参数错误", nil)
		return
	}
	ia, err := c.oar.GetOrg(dctx, orgid)
	if err != nil {
		c.ErrServerError(ctx, 2, "查找组织失败", errors.Wrapf(err, "检测组织(%v)是否存在失败：", orgid))
		return
	}
	if ia == nil {
		c.ErrNotFound(ctx, 3, "组织不存在", nil)
		return
	}

	c.Success(ctx, ia)
}

func (c orgCtrl) publish(ctx *gin.Context) {
	orgid := ctx.Param("orgid")
	if orgid == "" {
		c.ErrBadRequest(ctx, 1, "请求参数错误", nil)
		return
	}
	dctx := c.DDDContext(ctx)
	ia, err := c.oar.GetOrg(dctx, orgid)
	if err != nil {
		c.ErrServerError(ctx, 2, "发布失败", errors.Wrapf(err, "检测组织(%v)是否存在失败：", orgid))
		return
	}
	if ia == nil {
		c.ErrNotFound(ctx, 3, "组织不存在", nil)
		return
	}
	err = ia.Publish(dctx)
	if err != nil {
		c.ErrServerError(ctx, 4, "发布失败", errors.Wrapf(err, "发布组织(%v)失败：", orgid))
		return
	}
	if len(orgid) > 4 {
		t := orgid[0:3]
		if t == "002" {
			enid := orgid[4:]
			err := oper.GetHandle().UpdateEnterprisePanel(enid, 1)
			if err != nil {
				log.Warnf("激活企业一览中的展板开通状态失败:%v", err)
			}
		}
	}
	c.Success(ctx, nil)
}

func (c orgCtrl) nopublish(ctx *gin.Context) {
	orgid := ctx.Param("orgid")
	if orgid == "" {
		c.ErrBadRequest(ctx, 1, "请求参数错误", nil)
		return
	}
	dctx := c.DDDContext(ctx)
	ia, err := c.oar.GetOrg(dctx, orgid)
	if err != nil {
		c.ErrServerError(ctx, 2, "取消发布失败", errors.Wrapf(err, "检测组织(%v)是否存在失败：", orgid))
		return
	}
	if ia == nil {
		c.ErrNotFound(ctx, 3, "组织不存在", nil)
		return
	}
	err = ia.NoPublish(dctx)
	if err != nil {
		c.ErrServerError(ctx, 4, "取消发布失败", errors.Wrapf(err, "取消发布组织(%v)失败：", orgid))
		return
	}
	if len(orgid) > 4 {
		t := orgid[0:3]
		if t == "002" {
			enid := orgid[4:]
			err := oper.GetHandle().UpdateEnterprisePanel(enid, 0)
			if err != nil {
				log.Warnf("关闭企业一览中的展板开通状态失败:%v", err)
			}
		}
	}
	c.Success(ctx, nil)
}
